package com.cuitrip.business.track;

import android.os.Handler;
import android.os.Message;
import android.text.TextUtils;

import com.cuitrip.apiservice.TrackBusiness;
import com.lab.network.model.IProxyCallback;
import com.lab.network.model.ProxyResult;
import com.lab.network.proxy.ApiProxy;
import com.lab.utils.ArrayUtil;

import java.lang.ref.WeakReference;
import java.util.HashMap;
import java.util.List;

/**
 * 行程数据代理类
 * Created by ak on 15/9/23.
 */
public class TrackProxy implements IProxyCallback {


    private static TrackProxy sInstance;

    private ApiProxy apiProxy = new ApiProxy(this);

    private TrackHandler mTrackHandler;

    private static final class TrackHandler extends Handler {
        // Button clicks have Message.what as the BUTTON{1,2,3} constant
        public static final int MSG_REQUEST_TRACK = 1;

        private WeakReference<TrackProxy> mTrackReference;

        public TrackHandler(TrackProxy track) {
            mTrackReference = new WeakReference<>(track);
        }

        @Override
        public void handleMessage(Message msg) {
            TrackProxy trackProxy = mTrackReference.get();
            if (trackProxy == null) return;
            switch (msg.what) {
                case MSG_REQUEST_TRACK:
                    if (msg.obj instanceof HashMap) {
                        TrackBusiness.request(trackProxy.apiProxy, (HashMap<String, Object>) msg.obj);
                    }
                    break;
            }
        }
    }

    public static TrackProxy getInstance() {
        if (null == sInstance) {
            sInstance = new TrackProxy();
        }
        return sInstance;
    }

    private TrackProxy() {
        mTrackHandler = new TrackHandler(this);
    }

    public void requestTrack(HashMap<String, Object> dataParams) {
        mTrackHandler.removeMessages(TrackHandler.MSG_REQUEST_TRACK);
        Message message = Message.obtain();
        message.what = TrackHandler.MSG_REQUEST_TRACK;
        message.obj = dataParams;
        mTrackHandler.sendMessage(message);
    }


    public void reviewPage(TrackBusiness.Source source, String id) {
        HashMap<String, Object> dataParams = new HashMap<>();
        dataParams.put(TrackBusiness.SOURCE, source);
        dataParams.put(TrackBusiness.OP, TrackBusiness.Action.REVIEW);
        if (!TextUtils.isEmpty(id)) {
            dataParams.put(TrackBusiness.ITEM_ID, id);
        }
        mTrackHandler.removeMessages(TrackHandler.MSG_REQUEST_TRACK);
        Message message = Message.obtain();
        message.what = TrackHandler.MSG_REQUEST_TRACK;
        message.obj = dataParams;
        mTrackHandler.sendMessage(message);
    }


    public void operationAction(TrackBusiness.Source source, TrackBusiness.Action operationAction, String page, String index, List<String> itemIds) {
        HashMap<String, Object> dataParams = new HashMap<>();
        dataParams.put(TrackBusiness.SOURCE, source);
        dataParams.put(TrackBusiness.OP, operationAction);
        if (!TextUtils.isEmpty(page)) {
            dataParams.put(TrackBusiness.PAGE, page);
        }
        if (!TextUtils.isEmpty(index)) {
            dataParams.put(TrackBusiness.OPERATION_INDXE, index);
        }
        if (!ArrayUtil.isNullOrEmpty(itemIds)) {
            String ids = TextUtils.join(",", itemIds);
            dataParams.put(TrackBusiness.ITEM_ID, ids);
        }
        requestTrack(dataParams);
    }

    public void searchWords(TrackBusiness.Source source, String keyWord, String sourceWord) {
        HashMap<String, Object> dataParams = new HashMap<>();
        dataParams.put(TrackBusiness.SOURCE, source);
        dataParams.put(TrackBusiness.OP, TrackBusiness.Action.SEARCH);
        if (!TextUtils.isEmpty(keyWord)) {
            dataParams.put(TrackBusiness.KEY_WORD, keyWord);
        }
        if (!TextUtils.isEmpty(sourceWord)) {
            dataParams.put(TrackBusiness.HOT_WORD, sourceWord);
        }
        requestTrack(dataParams);
    }

    public String getCurrentPageIndex(int pageSize, int position) {
        return String.valueOf(position);
    }

    @Override
    public boolean onProxyResult(ProxyResult<?> result) {
        return true;
    }
}
